Smart Contract | How to Judge From and to Addresses Are Operations of Adding and Removing Liquidity

智能合约 | 如何判断from和to地址是添加和删除流动性的操作

Posted by Mr. Alex on 2023-08-11
Estimated Reading Time 1 Minutes
Words 303 In Total
Viewed Times

Overview | 业务场景:

最近遇到一个项目,需要发行一个ERC20 Token ,除去普通钱包转账,和Swap添加流动性和
删除流动性之外的transfer都需要收取手续费,这个时候我们需要写一个判断是否为添加流动
性和删除流动性的判断

具体判断的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
contract LiquidityActivity {

//check address is a add liquidity address
function isAddLiquidity(address from, address to, address liquidityPoolAddress) external view returns (bool) {
IUniswapV2Pair pool = IUniswapV2Pair(liquidityPoolAddress);

// Check if `from` is the liquidity pool contract and `to` is a user
if (from == liquidityPoolAddress && !isContract(to)) {
// Check if `to` holds both tokens of the pool (add liquidity)
return (to == pool.token0() || to == pool.token1());
}

return false;
}

//check address is remove liquidity address
function isRemoveLiquidity(address from, address to, address liquidityPoolAddress) external view returns (bool) {
IUniswapV2Pair pool = IUniswapV2Pair(liquidityPoolAddress);

// Check if `to` is the liquidity pool contract and `from` is a user
if (to == liquidityPoolAddress && !isContract(from)) {
// Check if `from` holds both tokens of the pool (remove liquidity)
return (from == pool.token0() || from == pool.token1());
}

return false;
}

// check address is a contract address
function isContract(address addr) internal view returns (bool) {
uint32 size;
assembly {
size := extcodesize(addr)
}
return (size > 0);
}
}
通过以上判断,就可以分辨出是否为添加和删除流动性操作,进而不对添加和删除流动性的操作收取手续费

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !